//eclipse external tools
//-x bash -c "cd /home/hassen/scripts/js/beautifier; php /home/hassen/scripts/js/beautifier/rhino.php /home/hassen/scripts/chrome${resource_path}"
var colorsOptions = null;
var localColors = false;


chrome.extension.sendRequest(
    {
        init: "init"
    },


    function(response)
    {
        if (response.ColorOptions)
        {
            colorsOptions = JSON.parse(response.ColorOptions);
            ConvertAll();
        }
    }
);

function optVal(strKey)
{
    if (colorsOptions !== null)
    {
        return colorsOptions[strKey][1];
    } else
    {
//        window.location.reload();
    }

}

function convertDom()
{
    var textColor = optVal("text_color");
    var backgroundColor = optVal("background_color");
    var linksColor = optVal("links_color");
    var nodes = document.getElementsByTagName('*');
    for (var i = 0; i < nodes.length; i++)
    {
        if (nodes[i].tagName == 'A')
        {
            nodes[i].style.color = linksColor;
        } else
        {
            nodes[i].style.color = textColor;
        }
        nodes[i].style.background = backgroundColor;
        nodes[i].style.backgroundImage.src = "";
    }
}

function convertBody()
{
    document.body.style.color = optVal("text_color");
    document.body.style.background = optVal("background_color");
    document.body.style.backgroundImage.src = "";
}

function ConvertAll(colorsChanged)
{
    convertBody();
    convertDom();
    colorsChanged = true;
}
var bodyInnerHTML = document.body.innerHTML;

function checkBody()
{
    if (document.body.innerHTML != bodyInnerHTML)
    {
        ConvertAll();
        bodyInnerHTML = document.body.innerHTML;
    }
}
window.setInterval('checkBody()', 300);